<?php


namespace app\admin\controller;


use app\admin\controller\BaseController;
use app\common\model\mysql\DriverFolder as DriverFolderModel;
use app\common\exception\MessageException;
use app\common\validate\NameValidate;

class DriveFolder extends BaseController
{
    public function list()
    {
        try {
            $params = $this->request->param();

//            if (!isset($params['folder_id']) || $params['folder_id'] == '') {
//                $params['folder_id'] = 0;
//            }
            $params['folder_id'] = $params['folder_id'] ?? 0;
            $result = DriverFolderModel::list($params);
            return $this->renderSuccess($result);
        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function treeList()
    {
        // 树结构查询
        $Model = new DriverFolderModel();
        $list = $Model->getTreeList();
        return $this->renderSuccess(compact('list'));
    }

    public function create()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }
        (new NameValidate())->goCheck();
        $params = $this->request->param();
        try {
            $Model = new DriverFolderModel();
            $findData = $Model->checkName($params['name']);

            if ($findData) {
                return $this->renderFail('分类名称已存在');
            }

            if (!$params['parent_id']) {
                $params['parent_id'] = 0;
            }
            $params['status'] = 1;
            $data = $Model->create($params);
            if ($data) {
                return $this->renderSuccess([], '添加成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function update()
    {
        if (!$this->request->isPost()) {
            throw new MessageException(['msg' => '请求方法不合法']);
        }

        (new NameValidate())->goCheck();
        $params = $this->request->param();

        if (!$params['id']) {
            return $this->renderFail('分类不存在');
        }

        try {
            $Model = new DriverFolderModel();
            $findData = $Model->checkName($params['name'], $params['id']);

            if ($findData) {
                return $this->renderFail('分类名称已存在');
            }

            if (!$params['parent_id']) {
                $params['parent_id'] = 0;
            }

            $data = $Model->update($params);

            if ($data) {
                return $this->renderSuccess([], '更新成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }

    public function delete()
    {
        $params = $this->request->param();
        if (!$params['id']) {
            return $this->renderFail('分类不存在');
        }
        try {
            $Model = new DriverFolderModel();
            $findData = $Model->where('id', $params['id'])->find();

            if (!$findData) {
                return $this->renderFail('分类不存在');
            }

            $data = $Model->update(['id' => $params['id'], 'status' => config('status.mysql.delete')]);
            if ($data) {
                return $this->renderSuccess([], '删除成功');
            }

        } catch (\Exception $e) {
            throw new MessageException(['msg' => $e->getMessage()]);
        }
    }
}